package org.homeunix.drummer.view.components;

import java.awt.Robot;

import javax.swing.SwingUtilities;

import org.homeunix.drummer.Buddi;
import org.homeunix.drummer.controller.MainBuddiFrame;
import org.homeunix.drummer.model.DataInstance;
import org.junit.Test;

import swingunit.extensions.ExtendedRobotEventFactory;
import swingunit.framework.EventPlayer;
import swingunit.framework.ExecuteException;
import swingunit.framework.FinderMethodSet;
import swingunit.framework.RobotEventFactory;
import swingunit.framework.Scenario;
import swingunit.framework.TestUtility;
import junit.framework.TestCase;

public class TestEditableTransaction extends TestCase{
  private Scenario scenario;
  private RobotEventFactory robotEventFactory = new ExtendedRobotEventFactory();
  private FinderMethodSet methodSet = new FinderMethodSet();
  private Robot robot;
  
  private Buddi application;
  
  protected void setUp() throws Exception {

    SwingUtilities.invokeAndWait(new Runnable() {
      public void run() {
        MainBuddiFrame.getInstance().openWindow();
//        application = new Buddi();
//        Buddi.launchGUI();
      }
    });
    
    robot = new Robot();
    TestUtility.waitForCalm();
    
    // To make sure to load the scenario file. 
    // SampleAppTest.xml is placed on the same package directory.
//    String filePath = Buddi.class.getResource("TestEditableTransaction.xml").getFile();

    // Create Scenario object and create XML file.
    scenario = new Scenario(robotEventFactory, methodSet);
    scenario.read("C:\\swingtest\\budget414\\src\\org\\homeunix\\drummer\\view\\components\\TestEditableTransaction.xml");
  }
  
  protected void tearDown() throws Exception {
    // Terminate application.
    Runnable r = new Runnable() {
      public void run() {
        if(application != null) {
//          application.setVisible(false);
        }
      }
    };
    SwingUtilities.invokeAndWait(r);

    application = null;
    scenario = null;
    robot = null;
  }
  @Test
  public void testNewAccount() throws ExecuteException {
    EventPlayer player = new EventPlayer(scenario);
    player.run(robot, "NEW_ACCOUNT");
    assertEquals("Checking balance 100.00", 10000, 
      DataInstance.getInstance().getAccounts().firstElement().getBalance());
  }
  @Test
  public void testNewTransaction() throws ExecuteException {
    EventPlayer player = new EventPlayer(scenario);
    player.run(robot, "NEW_TRANSACTION");
    assertEquals("Checking balance 99.00", 9900, 
      DataInstance.getInstance().getAccounts().firstElement().getBalance());
  }
  @Test
  public void testSplitTransaction() throws ExecuteException {
    EventPlayer player = new EventPlayer(scenario);
    player.run(robot, "SPLIT_TRANSACTION");
    assertEquals("Checking balance 90.00", 9000, 
      DataInstance.getInstance().getAccounts().firstElement().getBalance());
  }
}
